package com.doeis.common.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.voland.core.validator.Validator;

public class BeanUtils {
	
	public static boolean validate(Object obj,Map<String, Object> errors) {
		return validate(obj, errors, null);
	}
	
	/**
	 * 
	 * @param obj 需要验证的实体对象
	 * @param errors 返回时，返回页面所依赖的数据
	 * @return
	 */
	public static boolean validate(Object obj, Map<String, Object> errors, String...unValidateFields) {
		HashMap<String, String> re = Validator.validate(obj);
		//移除不需要验证的字段
		if(unValidateFields!=null && unValidateFields.length>0){
			for(String unValidate: unValidateFields){
				if(re.get(unValidate)!=null)
					re.remove(unValidate);
			}
		}
		
		if(re.size()>0){
			//设置字段的验证信息
			Set<String> fieldNames = re.keySet();
			String errorMessage = null;
			for(String fieldName : fieldNames){
				errorMessage = re.get(fieldName);
				errors.put(fieldName, errorMessage);
			}
		}
		
		return errors!=null && errors.size()>0;
	}
}
